﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using WebAPI.Entities;

namespace WebAPI.Dao;

public class CommentMapper
{
    private SqlCommandExecutor _sqlCommandExecutor = new SqlCommandExecutor();
    private TimeTransferUtils _transferUtils = new TimeTransferUtils();
    
    public List<Comment> SelectAll(string articleId)
    {
        List<Comment> list = new List<Comment>();
        Comment comment = null;

        string sql = "select * from [comment] where article_id=@articleId;";
        
        try
        {
            
            SqlCommand sqlCommand = _sqlCommandExecutor.PrepareSqlCommand(sql);
            sqlCommand.Parameters.AddWithValue("id", articleId);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            
            while (sqlDataReader.Read())
            {
                string id = (string)sqlDataReader["id"];
                string user_id = (string)sqlDataReader["user_id"];
                string article_id = (string)sqlDataReader["article_id"];
                string father_id = (string)sqlDataReader["father_id"];
                string content = (string)sqlDataReader["content"];
                string star = (string)sqlDataReader["star"];
                string create_time = _transferUtils.GetTimeString((DateTime)sqlDataReader["create_time"]);
                string update_time = _transferUtils.GetTimeString((DateTime)sqlDataReader["update_time"]);
            
                comment = new Comment(id, user_id, article_id, father_id, content, star, create_time, update_time);
                list.Add(comment);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return list;
    }

    public int Insert(Comment comment)
    {
        string sql = "insert [comment](id, user_id, article_id, father_id, content, star, create_time, update_time) values(@id, @user_id, @article_id, @father_id, @content, @star, @create_time, @update_time);";

        SqlCommand sqlCommand = _sqlCommandExecutor.PrepareSqlCommand(sql);
        
        sqlCommand.Parameters.AddWithValue("id", comment.Id);
        sqlCommand.Parameters.AddWithValue("user_id", comment.UserId);
        sqlCommand.Parameters.AddWithValue("article_id", comment.ArticleId);
        sqlCommand.Parameters.AddWithValue("father_id", comment.FatherId);
        sqlCommand.Parameters.AddWithValue("content", comment.Content);
        sqlCommand.Parameters.AddWithValue("star", comment.Star);
        sqlCommand.Parameters.AddWithValue("create_time", comment.CreateTime);
        sqlCommand.Parameters.AddWithValue("update_time", comment.UpdateTime);
        
        int flag = 0;
        
        try
        {
            flag = sqlCommand.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return flag;
    }
    
    public int Update(Comment comment)
    {
        string sql = "update [comment] set id=@id, user_id=@user_id, title=@title, inner_text=@inner_text, tag=@tag, star=@star, watcher=@watcher, update_time=@update_time;";

        SqlCommand sqlCommand = _sqlCommandExecutor.PrepareSqlCommand(sql);
        
        sqlCommand.Parameters.AddWithValue("id", comment.Id);
        sqlCommand.Parameters.AddWithValue("user_id", comment.UserId);
        sqlCommand.Parameters.AddWithValue("article_id", comment.ArticleId);
        sqlCommand.Parameters.AddWithValue("father_id", comment.FatherId);
        sqlCommand.Parameters.AddWithValue("content", comment.Content);
        sqlCommand.Parameters.AddWithValue("star", comment.Star);
        sqlCommand.Parameters.AddWithValue("update_time", comment.UpdateTime);
        
        int flag = 0;
        
        try
        {
            flag = sqlCommand.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return flag;
    }
    
    public int Delete(Comment comment)
    {
        string sql = "delete from [comment] where id=@id;";

        SqlCommand sqlCommand = _sqlCommandExecutor.PrepareSqlCommand(sql);
        
        sqlCommand.Parameters.AddWithValue("id", comment.Id);
        
        int flag = 0;
        
        try
        {
            flag = sqlCommand.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return flag;
    }
}